package route

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"im/app/conf"
	"im/app/controllers"
	"im/app/route/middleware"
)

func InitRoute() {
	r := gin.Default()
	r.Use(middleware.Cors()) //所有请求先cors

	r.GET("/test", func(c *gin.Context) {

		conf.Log.WithFields(logrus.Fields{
			"animal": "walrus",
			"size":   10,
		}).Info("A group of walrus emerges from the ocean")

		//controllers.ClientMap.Range(func(key, value interface{}) bool {
		//	fmt.Println(key, value)
		//	return true
		//})
		//
		//c.JSON(200, gin.H{
		//	"message": "hello",
		//	"data": "",
		//})
	})

	// 访问静态资源
	r.Static("/uploads", "./uploads")

	// 登录认证控制器
	auth := new(controllers.Auth)
	authW :=r.Group("/auth")
	{
		authW.POST("/login", auth.Login)
		authW.POST("/register", auth.Register)
	}

	authGroup :=r.Group("/")
	authGroup.Use(middleware.TokenCheck())
	{
		// 用户
		authGroup.GET("/auth/user-info", auth.GetUserInfo) // 获取用户信息

		// 聊天控制器
		chat := new(controllers.Chat)
		authGroup.GET("/chat/chat", chat.Chat)

		// 消息
		message := new(controllers.MessageController)
		authGroup.POST("/message/add", message.Add)
		authGroup.POST("/message/ack", message.Ack)
		authGroup.POST("/message/outlive", message.GetOutLiveMsg)

		// 群控制器
		userGroup := new(controllers.UserGroup)
		authGroup.POST("/user_group/add", userGroup.Add)   // 创建群
		authGroup.POST("/user_group/join", userGroup.JoinGroup) // 加入群
		authGroup.POST("/user_group/out", userGroup.OutGroup)   // 退群
		authGroup.POST("/user_group/search", userGroup.Search)  // 搜索群

		// 文件上次控制器
		upload := new(controllers.Upload)
		authGroup.POST("/upload/file", upload.UploadFile) // 文件上传

		// 好友
		friend := new(controllers.Friends)
		authGroup.GET("/friends/list", friend.List)  // 好友列表
		authGroup.GET("/friends/info", friend.InfoFriend)  // 好友信息
		authGroup.POST("/friends/search", friend.Search) // 搜索好友
		authGroup.POST("/friends/add", friend.Add) // 添加好友
		authGroup.POST("/friends/add-name", friend.AddByName) // 添加好友
		authGroup.POST("/friends/delete", friend.Delete) // 删除好友
	}


	port := viper.GetString("app.http.port")
	r.Run(port)
}
